package com.zp.qualitypractice.list.chain;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.zp.qualitypractice.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by izp on 2017/9/6.
 */

public class ChainRecyclerDemoActivity extends AppCompatActivity {
    private RecyclerView mRv;

    private ChainRecyclerAdapter<String> mAdapter;
    private List<String> mList = new ArrayList<>();

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chain_recycler_demo);
        initView();
        initData();
    }

    private void initView() {

        mRv = (RecyclerView) findViewById(R.id.rvMain);

        mAdapter = new ChainRecyclerAdapter<String>(this, mList,true) {
            @Override
            public void convert(ChainViewHolder holder, final String s, int layoutType,
                                final int position) {
                //只有一种布局，不使用layoutType来区分type了
                if (layoutType == R.layout.item_rv_main_more){
                    Toast.makeText(ChainRecyclerDemoActivity.this, "正在加载更多",
                            Toast.LENGTH_SHORT).show();
                }else {
                    holder//
                            .setText(R.id.tv_content, s)
                            .setOnItemClickListenner(new View.OnClickListener() {
                                @Override public void onClick(View v) {
                                    Toast.makeText(ChainRecyclerDemoActivity.this, "item点击:" + s,
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                }
            }

            @Override
            public int getLayoutAsViewType(String s, int position) {
//                这里不仅可以通过position来判断该返回什么类型的布局，还可以根据data来判断。
                if (position==mList.size()){
                    return R.layout.item_rv_main_more;
                }else {
                    return R.layout.item_rv_main;
                }
            }
        };

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(layoutManager);
        mRv.setAdapter(mAdapter);
    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            mList.add("vicky-" + i);
        }
        mAdapter.notifyDataSetChanged();
    }
}
